"""
Author        yongfa
Date          2022-07-18 10:56:47
LastEditTime  2022-07-18 11:45:47
LastEditors   yongfa
Description   模型数据
"""

from app.modules.v1.permission.models import Apis


from faker import Faker

fake = Faker(locale='zh_CN')


class PermissionFake():

    @staticmethod
    def fake_apis(count=10):
        """模拟api数据

        Args:
            count(int, optional): _description_. Defaults to 10.
        """
        levels = 4

        def fake_data(count, level=1, parent=[]):
            if level <= levels:
                apis = []
                for i in range(count):
                    if not parent:
                        api = Apis(
                            name=fake.name() + '_' + str(i),
                            label=fake.name(),
                            method=fake.random_element(elements=('GET', 'POST', 'PUT', 'DELETE')),
                        )
                        print(api)
                        api.save()
                        apis.append(api)
                    else:
                        for item in parent:
                            api = Apis(
                                name=fake.name() + "_{}_{}".format(item.name, i),
                                label=fake.name(),
                                method=fake.random_element(elements=('GET', 'POST', 'PUT', 'DELETE')),
                                parent_id=item.id
                            )
                            print(api)
                            api.save()
                            apis.append(api)

                level += 1
                fake_data(count, level, apis)

        fake_data(count)
